home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  13.6 KB  |  666 lines

  1. /* Main network program - provides both client and server functions */
  2. #include <stdio.h>
  3. #include <time.h>
  4. #ifdef    __TURBOC__
  5. #include <io.h>
  6. #endif
  7. #include "global.h"
  8. #include "config.h"
  9. #include <stdarg.h>
  10. #include "files.h"
  11. #include "mbuf.h"
  12. #include "socket.h"
  13. #include "iface.h"
  14. #include "ftpcli.h"
  15. #include "telnet.h"
  16. #include "remote.h"
  17. #include "session.h"
  18. #include "cmdparse.h"
  19. #include "ax25.h"
  20. #include "kiss.h"
  21. #include "enet.h"
  22. #include "timer.h"
  23. #include "proc.h"
  24. #include "tty.h"
  25. #include "daemon.h"
  26. #include "usock.h"
  27. #include "netrom.h"
  28. #include "ip.h"
  29. #include "tcp.h"
  30. #include "udp.h"
  31. #include "hardware.h"
  32. #include "commands.h"
  33. #include <dos.h>
  34. #include <stdlib.h>
  35. #include <ios1.h>
  36. #include <proto/dos.h>
  37.  
  38. #ifdef    AMIGA
  39. #include "amiga/windows.h"
  40.  
  41. extern int WBorder;
  42. extern int WResize;
  43. extern int amiga_winsizes[4];
  44. extern long amiga_wintype;
  45. #endif
  46.  
  47. extern struct cmds Cmds[],Startcmds[],Stopcmds[],Attab[];
  48. extern int32 Heapsize;
  49.  
  50. static char Escape = 0x1d;    /* default escape character is ^] */
  51.  
  52. char Badhost[] = "Unknown host %s\n";
  53. char Badinterface[] = "Interface \"%s\" unknown\n";
  54.  
  55. char *Hostname;
  56. char Nospace[] = "No space!!\n";    /* Generic malloc fail message */
  57.  
  58. struct proc *Cmdpp;
  59. struct tm *stime;
  60. int Console;
  61. struct proc *Display;
  62. struct session *Command;
  63. static time_t StartTime;        /* Time that NOS was started */
  64. static int Verbose;
  65. extern int Attended;
  66. extern char *Motd;
  67. char *curftwin;
  68.  
  69. int main(argc,argv)
  70. int argc;
  71. char *argv[];
  72. {
  73.     char *inbuf,*intmp;
  74.     FILE *fp;
  75.     struct daemon *tp;
  76.     struct mbuf *bp;
  77.     int rootset, c;
  78.  
  79.     StartTime = time(&StartTime);            /* NOS Start_Up time */
  80.  
  81.     curftwin = "0023";
  82.  
  83.     rootset = 0;
  84.     while((c = getopt(argc,argv,"s:d:m:l:t:w:h:vbr?")) != EOF){
  85.         switch(c) {
  86.         case '?': /* Options HELP */
  87.             printf("\n  AmigaNOS Command-Line Options:\n\n");
  88.             printf("  -l<nnn>     Left edge     nnn - pixels\n");
  89.             printf("  -t<nnn>     Top Edge      nnn - pixels\n");
  90.             printf("  -w<nnn>     Width         nnn - pixels\n");
  91.             printf("  -h<nnn>     Height        nnn - pixels\n\n");
  92.             printf("  -b          Borderless Windows\n");
  93.             printf("  -r          Window Resize Gadgets\n\n");
  94.             printf("  -s<nn>      Number of sockets\n");
  95.             printf("  -d<sssssss> AmigaNOS Root device/directory\n");
  96.             printf("  -m<nnn>     Heap memory size\n\n");
  97.             printf("  -v          Verbose flag\n\n");
  98.             exit(0);
  99.         case 'l': /* Left edge */
  100.             amiga_winsizes[0] = atoi(optarg);
  101.             break;
  102.         case 't': /* Top edge */
  103.             amiga_winsizes[1] = atoi(optarg);
  104.             break;
  105.         case 'w': /* Width */
  106.             amiga_winsizes[2] = atoi(optarg);
  107.             break;
  108.         case 'h': /* Height */
  109.             amiga_winsizes[3] = atoi(optarg);
  110.             break;
  111.         case 'b': /* Border */
  112.             WBorder = 0;
  113.             break;
  114.         case 'r': /* Resize */
  115.             WResize = 0;
  116.             break;
  117.         case 's': /* Number of sockets */
  118.             Nusock = atoi(optarg);
  119.             break;
  120.         case 'd': /* Root directory for various files */
  121.             initroot(optarg);
  122.             rootset = 1;
  123.             break;
  124.         case 'm': /* Heap memory size */
  125.             Heapsize = 1024 * atol(optarg);
  126.             break;
  127.         case 'v': /* Verbose flag */
  128.             Verbose = 1;
  129.             break;
  130.         }
  131.     }
  132.     if(!rootset) {
  133.         initroot("TCPIP");
  134.     }
  135.     kinit();
  136.     ioinit(Heapsize);
  137.     sockinit();
  138.     Cmdpp = mainproc("cmdintrp");
  139.     mainlog(-1,"Start NOS v%s", Version);
  140.  
  141.     Sessions = (struct session *)callocw(Nsessions,sizeof(struct session));
  142.     Command = Lastcurr = newsession("command interpreter",COMMAND);
  143.     Display = newproc("display",250,display,0,NULLCHAR,NULL);
  144.     tprintf("KA9Q Internet Protocol Package, v%s\n",Version);
  145.     tprintf("Copyright 1989 by Phil Karn, KA9Q\n");
  146.     fflush(stdout);
  147.  
  148.     if(optind < argc){
  149.         /* Read startup file named on command line */
  150.         if((fp = fopen(argv[optind],READ_TEXT)) == NULLFILE)
  151.             tprintf("Can't read config file %s: %s\n",
  152.              argv[optind],sys_errlist[errno]);
  153.     } else {
  154.         fp = fopen(Startup,READ_TEXT);
  155.     }
  156.     if(fp != NULLFILE){
  157.         inbuf = mallocw(BUFSIZ);
  158.         intmp = mallocw(BUFSIZ);
  159.         while(fgets(inbuf,BUFSIZ,fp) != NULLCHAR){
  160.             strcpy(intmp,inbuf);
  161.             if(Verbose)
  162.                 tprintf("%s",intmp);
  163.             if(cmdparse(Cmds,inbuf,NULL) != 0){
  164.                 tprintf("input line: %s",intmp);
  165.             }
  166.         }
  167.         fclose(fp);
  168.         free(inbuf);
  169.         free(intmp);
  170.     }
  171.     /* Start background Daemons */
  172.     for(tp=Daemons;;tp++){
  173.         if(tp->name == NULLCHAR)
  174.             break;
  175.         newproc(tp->name,tp->stksize,tp->fp,0,NULLCHAR,NULL);
  176.     }
  177.     /* Now loop forever, processing commands */
  178.     for(;;){
  179.                 long currtime;
  180.                 time(&currtime);
  181.                 stime = localtime(&currtime);
  182.                 tprintf("\x0f\x1b[32mNOS <\x1b[33m%02d:%02d - %s\x1b[32m> \x1b[0m",
  183.                         stime->tm_hour, stime->tm_min, Hostname);
  184.         usflush(Command->output);
  185.         if(recv_mbuf(Command->input,&bp,0,NULLCHAR,0) != -1){
  186.             (void)cmdparse(Cmds,bp->data,Lastcurr);
  187.             free_p(bp);
  188.         }
  189.     }
  190. }
  191.  
  192. /* Keyboard input process */
  193. void keyboard(i,v1,v2)
  194. int i;
  195. void *v1;
  196. void *v2;
  197. {
  198.     int c;
  199.     struct mbuf *bp;
  200.  
  201.     /* Keyboard process loop */
  202.     for(;;){
  203.         c = kbread();
  204.         if(c == Escape && Escape != 0)
  205.             c = -2;
  206.         if(c == -2 && Current != Command){
  207.             /* Save current tty mode and set cooked */
  208.             swapscreen(Current,Command);
  209.             Lastcurr = Current;
  210.             Current = Command;
  211.         }
  212.         Current->row = MOREROWS;
  213.         psignal(&Current->row,1);
  214.         if(c >= 0){
  215.             /* If the screen driver was in morewait state, this char
  216.              * has woken him up. Toss it so it doesn't also get taken
  217.              * as normal input. If the char was a command escape,
  218.              * however, it will be accepted; this gives the user
  219.              * a way out of lengthy output.
  220.              */
  221.             if(!Current->morewait
  222.              && (bp = ttydriv(Current,(char)c)) != NULLBUF){
  223.                 if(Current->record != NULLFILE
  224.                  && Current->ttystate.echo)
  225.                     write_p(Current->record,bp);
  226.                 send_mbuf(Current->input,bp,0,NULLCHAR,0);
  227.             }
  228.         }
  229.     }
  230. }
  231.  
  232. int doexit(argc,argv,p)
  233. int argc;
  234. char *argv[];
  235. void *p;
  236. {
  237.     mainlog(-1,"Stop  NOS v%s", Version);
  238.     reset_all();
  239.     iostop();
  240.     exit(0);
  241.     return 0;    /* To satisfy lint */
  242. }
  243.  
  244. int dohostname(argc,argv,p)
  245. int argc;
  246. char *argv[];
  247. void *p;
  248. {
  249.     if(argc < 2)
  250.         tprintf("%s\n",Hostname);
  251.     else {
  252.         if(Hostname != NULLCHAR)
  253.             free(Hostname);
  254.         Hostname = strdup(argv[1]);
  255.     }
  256.     return 0;
  257. }
  258.  
  259. int dohelp(argc,argv,p)
  260. int argc;
  261. char *argv[];
  262. void *p;
  263. {
  264.     register struct cmds *cmdp;
  265.     int i;
  266.     char buf[77];
  267.  
  268.     if(*argv[0] == '?' ) {
  269.         tprintf("Main commands:\n");
  270.         memset(buf,' ',sizeof(buf));
  271.         buf[75] = '\n';
  272.         buf[76] = '\0';
  273.         for(i=0,cmdp = Cmds;cmdp->name != NULL;cmdp++,i = (i+1)%5) {
  274.             strncpy(&buf[i*15],cmdp->name,strlen(cmdp->name));
  275.             if(i == 4){
  276.                 tprintf(buf);
  277.                 memset(buf,' ',sizeof(buf));
  278.                 buf[75] = '\n';
  279.                 buf[76] = '\0';
  280.             }
  281.         }
  282.         if(i != 0)
  283.             tprintf(buf);
  284.         return 0;
  285.     } else {
  286.         buf[0] = '\0';
  287.         if(argc > 1)
  288.             for(i=0; Cmds[i].name != NULLCHAR; ++i)
  289.                 if(!strncmp(Cmds[i].name,
  290.                     argv[1],strlen(argv[1]))) {
  291.                     sprintf(buf,"ARUN LESS %s/main.%s NOIO",
  292.                         Helpdir,Cmds[i].name);
  293.                     if (!Execute(buf, 0, 0)) {
  294.                         return (FILE *)NULL;
  295.                     }
  296.                     break;
  297.                 }
  298.         return 0;
  299.     }
  300. }
  301.  
  302. /* Attach an interface
  303.  * Syntax: attach <hw type> <I/O address> <vector> <mode> <label> <bufsize> [<speed>]
  304.  */
  305. int doattach(argc,argv,p)
  306. int argc;
  307. char *argv[];
  308. void *p;
  309. {
  310.     return subcmd(Attab,argc,argv,p);
  311. }
  312.  
  313. /* Manipulate I/O device parameters */
  314. int doparam(argc,argv,p)
  315. int argc;
  316. char *argv[];
  317. void *p;
  318. {
  319.     register struct iface *ifp;
  320.  
  321.     if((ifp = if_lookup(argv[1])) == NULLIF){
  322.         tprintf(Badinterface,argv[1]);
  323.         return 1;
  324.     }
  325.     if(ifp->ioctl == NULLFP){
  326.         tprintf("Not supported\n");
  327.         return 1;
  328.     }
  329.     /* Pass rest of args to device-specific code */
  330.     return (*ifp->ioctl)(ifp,argc-2,argv+2);
  331. }
  332.  
  333. /* Display or set IP interface control flags */
  334. int domode(argc,argv,p)
  335. int argc;
  336. char *argv[];
  337. void *p;
  338. {
  339.     register struct iface *ifp;
  340.  
  341.     if((ifp = if_lookup(argv[1])) == NULLIF) {
  342.         tprintf(Badinterface, argv[1]);
  343.         return 1;
  344.     }
  345.     if(argc < 3){
  346.         tprintf("%s: %s\n",ifp->name,
  347.          (ifp->flags & CONNECT_MODE) ? "VC mode" : "Datagram mode");
  348.         return 0;
  349.     }
  350.     switch(argv[2][0]){
  351.     case 'v':
  352.     case 'c':
  353.     case 'V':
  354.     case 'C':
  355.         ifp->flags = CONNECT_MODE;
  356.         break;
  357.     case 'd':
  358.     case 'D':
  359.         ifp->flags = DATAGRAM_MODE;
  360.         break;
  361.     default:
  362.         tprintf("Usage: %s [vc | datagram]\n",argv[0]);
  363.         return 1;
  364.     }
  365.     return 0;
  366. }
  367.  
  368. int doescape(argc,argv,p)
  369. int argc;
  370. char *argv[];
  371. void *p;
  372. {
  373.     if(argc < 2)
  374.         tprintf("0x%x\n",Escape);
  375.     else 
  376.         Escape = *argv[1];
  377.     return 0;
  378. }
  379.  
  380. /* Generate system command packet. Synopsis:
  381.  * remote [-p port#] [-k key] [-a hostname] <hostname> reset|exit|kickme
  382.  */
  383. int doremote(argc,argv,p)
  384. int argc;
  385. char *argv[];
  386. void *p;
  387. {
  388.     struct sockaddr_in fsock;
  389.     int s,c;
  390.     char *data,x;
  391.     int16 port,len;
  392.     char *key = NULLCHAR;
  393.     int klen;
  394.     int32 addr = 0;
  395.     char *cmd,*host;
  396.  
  397.     port = IPPORT_REMOTE;    /* Set default */
  398.     optind = 1;        /* reinit getopt() */
  399.     while((c = getopt(argc,argv,"a:p:k:s:")) != EOF){
  400.         switch(c){
  401.         case 'a':
  402.             addr = resolve(optarg);
  403.             break;
  404.         case 'p':
  405.             port = atoi(optarg);
  406.             break;
  407.         case 'k':
  408.             key = optarg;
  409.             klen = strlen(key);
  410.             break;
  411.         case 's':
  412.             Rempass = strdup(optarg);
  413.             return 0;    /* Only set local password */
  414.         }
  415.     }
  416.     if(optind > argc - 2){
  417.         tprintf("Insufficient args\n");
  418.         return -1;
  419.     }
  420.     host = argv[optind++];
  421.     cmd = argv[optind];
  422.     if((s = socket(AF_INET,SOCK_DGRAM,0)) == -1){
  423.         tprintf("socket failed\n");
  424.         return 1;
  425.     }
  426.     len = 1;
  427.     /* Did the user include a password or kickme target? */
  428.     if(addr != 0)
  429.         len += sizeof(int32);
  430.  
  431.     if(key != NULLCHAR)
  432.         len += klen;
  433.  
  434.     if(len == 1)
  435.         data = &x;
  436.     else
  437.         data = mallocw(len);
  438.  
  439.     fsock.sin_family = AF_INET;
  440.     fsock.sin_addr.s_addr = resolve(host);
  441.     fsock.sin_port = port;
  442.  
  443.     switch(cmd[0]){
  444.     case 'r':
  445.         data[0] = SYS_RESET;
  446.         if(key != NULLCHAR)
  447.             strncpy(&data[1],key,klen);
  448.         break;
  449.     case 'e':
  450.         data[0] = SYS_EXIT;
  451.         if(key != NULLCHAR)
  452.             strncpy(&data[1],key,klen);
  453.         break;
  454.     case 'k':
  455.         data[0] = KICK_ME;
  456.         if(addr != 0)
  457.             put32(&data[1],addr);
  458.         break;
  459.     default:
  460.         tprintf("Unknown command %s\n",cmd);
  461.         goto cleanup;
  462.     }
  463.     /* Form the command packet and send it */
  464.     if(sendto(s,data,len,0,(char *)&fsock,sizeof(fsock)) == -1){
  465.         tprintf("sendto failed: %s\n",sys_errlist[errno]);
  466.         goto cleanup;
  467.     }
  468. cleanup:
  469.     if(data != &x)
  470.         free(data);
  471.     close_s(s);
  472.     return 0;
  473. }
  474.  
  475. int doless(argc,argv,p)
  476. int argc;
  477. char *argv[];
  478. void *p;
  479. {
  480.     char buf[81];
  481.  
  482.     sprintf(buf,"ARUN LESS %s NOIO",argv[1]);
  483.     if (!Execute(buf, 0, 0)) {
  484.         return (FILE *)NULL;
  485.     }
  486.     return 0;
  487. }
  488.  
  489. /* Log messages of the form
  490.  * 00:00:00 44.64.0.7:1003 open FTP
  491.  */
  492. void mainlog(int s,char *fmt, ...)
  493. {
  494.     va_list ap;
  495.     char *cp, *da, *mo, *yr, *ti, ML[128];
  496.     long t;
  497.     int i;
  498.     struct sockaddr fsocket;
  499.     FILE *fp;
  500.  
  501.     time(&t);
  502.     cp = ctime(&t);
  503.     rip(cp);
  504.  
  505.     strtok(cp," ");    /* Day   */
  506.     mo = strtok(NULL," ");  /* Month */
  507.     da = strtok(NULL," ");  /* Date  */
  508.     ti = strtok(NULL," ");  /* Time  */
  509.     yr = strtok(NULL," ");  /* Year  */
  510.  
  511.     sprintf(ML, "%s/%s-%s-%s", LogsDir, mo, da, yr);
  512.  
  513.     if((fp = fopen(ML,APPEND_TEXT)) == NULLFILE)
  514.         return;
  515.  
  516.     i = SOCKSIZE;
  517.  
  518.     fprintf(fp,"%s ",ti);
  519.     if(getpeername(s,(char *)&fsocket,&i) != -1)
  520.         fprintf(fp," %s",psocket(&fsocket));
  521.     fprintf(fp," - ");
  522.     va_start(ap,fmt);
  523.     vfprintf(fp,fmt,ap);
  524.     va_end(ap);
  525.     fprintf(fp,"\n");
  526.     fclose(fp);
  527. }
  528.  
  529. int dosource(argc,argv,p)
  530. int argc;
  531. char *argv[];
  532. void *p;
  533. {
  534.     int linenum = 0;
  535.     char *inbuf,*intmp;
  536.     FILE *fp;
  537.  
  538.     /* Read command source file */
  539.     if((fp = fopen(argv[1],READ_TEXT)) == NULLFILE){
  540.         tprintf("Can't read source file %s: %s\n",
  541.          argv[1],sys_errlist[errno]);
  542.         return 1;
  543.     }
  544.  
  545.     inbuf = malloc(BUFSIZ);
  546.     intmp = malloc(BUFSIZ);
  547.     while(fgets(inbuf,BUFSIZ,fp) != NULLCHAR){
  548.         strcpy(intmp,inbuf);
  549.         linenum++;
  550.         if(Verbose)
  551.             tprintf("%s",intmp);
  552.         if(cmdparse(Cmds,inbuf,NULL) != 0){
  553.             tprintf("*** file \"%s\", line %d: %s\n",
  554.                 argv[1],linenum,intmp);
  555.         }
  556.     }
  557.     fclose(fp);
  558.     free(inbuf);
  559.     free(intmp);
  560.     return 0;
  561. }
  562.  
  563. dostatus(argc,argv,p)
  564. int argc;
  565. char *argv[];
  566. void *p;
  567. {
  568.     time_t nowtime, elapsedtime;
  569.     unsigned int days,hrs,mins,secs;
  570.  
  571.     nowtime = time(&nowtime);            /* current time */
  572.     elapsedtime = nowtime - StartTime;        /* nos elapsed time */
  573.  
  574.     tprintf("KA9Q Internet Protocol Package, v%s\n\n",Version);
  575.     tprintf("The system time is %s", ctime(&nowtime));
  576.     tprintf("NOS was started on %s\n", ctime(&StartTime));
  577.     secs = elapsedtime % 60;
  578.     elapsedtime = elapsedtime / 60;
  579.     mins = elapsedtime % 60;
  580.     elapsedtime = elapsedtime / 60;
  581.     hrs = elapsedtime % 24;
  582.     elapsedtime = elapsedtime / 24;
  583.     days = elapsedtime;
  584.     tprintf("Elapsed time => %u days: %02u hours: %02u minutes: %02u seconds.\n\n",days,hrs,mins,secs);
  585.     tprintf("The station is currently %sttended.\n", Attended ? "A" : "Una");
  586.     tprintf("The 'Message Of The Day' is ");
  587.     if(Motd != NULLCHAR)
  588.         tprintf("\n%s",Motd);
  589.     else
  590.         tprintf("not set!\n");
  591. #ifdef    __TURBOC__
  592.     dofstat();        /* print status of open files */
  593. #endif
  594.     tprintf("\nNOS configuration information.\ncontaining ");
  595. #ifdef SERVERS
  596.     tprintf(" TCP servers,");
  597. #endif
  598. #ifdef POP
  599.     tprintf(" POP2,");
  600. #endif
  601. #ifdef NNTP
  602.     tprintf(" NNTP,");
  603. #endif
  604. #ifdef RIP
  605.     tprintf(" RIP,");
  606. #endif
  607. #ifdef RSPF
  608.     tprintf(" RSPF,");
  609. #endif
  610. #ifdef    BOOTP
  611.     tprintf(" BOOTP,");
  612. #endif
  613. #ifdef HOPCHECK
  614.     tprintf(" HOP,");
  615. #endif
  616. #ifdef MAILBOX
  617.     tprintf(" MAILBOX");
  618. #endif
  619. #ifdef    ASY
  620.     tprintf("\nGeneric async interface\n");
  621. #endif
  622. #ifdef    SLIP
  623.     tprintf("SLIP async interface\n");
  624. #endif
  625. #ifdef    KISS
  626.     tprintf("KISS async interface\n");
  627. #endif
  628. #ifdef    NRS
  629.     tprintf("NET/ROM async interface\n");
  630. #endif
  631. #ifdef    NETROM
  632.     tprintf("NET/ROM network interface\n");
  633. #endif
  634. #ifdef DIGGER
  635.     tprintf("Digger by WA2ZZX/G1YYH\n");
  636. #endif
  637. #ifdef    CHATNODE
  638.     tprintf("ChatNode by DK5SG/G1YYH\n");
  639. #endif
  640. #ifdef    PPP
  641.     tprintf("Point to Point network interface\n");
  642. #endif
  643. #ifdef    VJCOMPRESS
  644.     tprintf("Van Jacobson compression on SLIP/PPP\n");
  645. #endif
  646. #ifdef    FORTH
  647.     tprintf("Forth programming language\n");
  648. #endif
  649.     return 0;
  650. }
  651.  
  652. /* Set up FTP/Download window */
  653. int doftpdown(argc,argv,p)
  654. int argc;
  655. char *argv[];
  656. void *p;
  657. {
  658.     if(argc == 1)
  659.         tprintf("Current FTP/Download Window = %s\n", curftwin);
  660.     else {
  661.         strcpy(curftwin,argv[1]);
  662.         tprintf("New FTP/Download Window = %s \n", curftwin);
  663.     }
  664.     return 0;
  665. }
  666.